#!/usr/bin/env python
#coding=utf-8
from wtforms import Form, validators
from wtforms.fields import BooleanField, IntegerField, TextField, TextAreaField, PasswordField, SelectField, SelectMultipleField
from wtforms.ext.sqlalchemy.fields import QuerySelectMultipleField
from happypress.models import db,Category

__all__ = ['loginForm','postForm','categoryForm']

v_required = validators.Required(message=u'必填')
v_slug = validators.Regexp(r'^[a-zA-Z0-9_\-\s]+$',message=u'只支持字母、数字、空格、中划线和下划线')
v_email = validators.Email(message=u'请输入正确的邮箱')
v_num = validators.Regexp(r'^\d+$',message=u'请输入数字')

def enabled_categories():
    return db.session.query(Category).order_by('sort_order')

class loginForm(Form):
    email = TextField(u'邮箱', [v_email])
    password = PasswordField(u'密码')

class categoryForm(Form):
    title = TextField(u'标题', [v_required])
    slug = TextField('Slug', [v_required,v_slug])
    sort_order = TextField(u'排序', [v_num], default='99')
    
class postForm(Form):
    title = TextField(u'标题', [v_required])
    slug = TextField('Slug', [v_required,v_slug])
    excerpt = TextAreaField(u'摘记', [v_required])
    body = TextAreaField(u'内容', [v_required])
    categories = QuerySelectMultipleField(u'分类',query_factory=enabled_categories, get_label='title', allow_blank=True)
    enable_comments = BooleanField(u'允许评论')
    featured = BooleanField(u'是否推荐')
    
